import { defineStore } from 'pinia';

export const useImageStore = defineStore('image', {
    state: () => ({
        uploadedImageUrl: null,
        detectedImageUrl: null,
        uploadedImage: false,
        loading: false,
        confirmed: false,
    }),
    actions: {
        setUploadedImageUrl(url) {
            this.uploadedImageUrl = url;
            this.uploadedImage = true;
            localStorage.setItem('uploadedImageUrl', url);
        },
        setDetectedImageUrl(url) {
            this.detectedImageUrl = url;
            this.confirmed = true;
            localStorage.setItem('detectedImageUrl', url);
        },
        resetState() {
            this.uploadedImageUrl = null;
            this.detectedImageUrl = null;
            this.uploadedImage = false;
            this.loading = false;
            this.confirmed = false;
            localStorage.removeItem('uploadedImageUrl');
            localStorage.removeItem('detectedImageUrl');
        },
        restoreState() {
            const storedUploadedImageUrl = localStorage.getItem('uploadedImageUrl');
            if (storedUploadedImageUrl) {
                this.setUploadedImageUrl(storedUploadedImageUrl);
            }

            const storedDetectedImageUrl = localStorage.getItem('detectedImageUrl');
            if (storedDetectedImageUrl) {
                this.setDetectedImageUrl(storedDetectedImageUrl);
            }
        },
    },
    persist: {
        enabled: true,
        strategies: [
            {
                key: 'image',
                storage: localStorage,
                paths: ['uploadedImageUrl', 'detectedImageUrl'],
            },
        ],
    },
});